<?php

namespace NuitInfo2013\MainBundle\Controller;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;

use NuitInfo2013\MainBundle\Entity\Topic;
use NuitInfo2013\MainBundle\Form\TopicType;

/**
 * Topic controller.
 *
 */
class TopicController extends Controller
{

    /**
     * Lists all Topic entities.
     *
     */
    public function indexAction()
    {
        $em = $this->getDoctrine()->getManager();
        $user = $this->container->get('security.context')->getToken()->getUser();
        $userId = ($this->container->get('security.context')->isGranted('IS_AUTHENTICATED_FULLY')) ? $user->getId() :0;
        
        $entities = $em->getRepository('NuitInfo2013MainBundle:Topic')->findByUserId($userId);

        return $this->render('NuitInfo2013MainBundle:Topic:index.html.twig', array(
            'entities' => $entities,
        ));
    }

     /**
     * Lists all Topic entities.
     *
     */
    public function switchAction($id)
    {
        $em = $this->getDoctrine()->getManager();

        $entity = $em->getRepository('NuitInfo2013MainBundle:Topic')->find($id);
        $entity->setStatut(!$entity->getStatut());
        $em->persist($entity);
        $em->flush();
        return $this->indexAction();
    }




    /**
     * Creates a new Topic entity.
     *
     */
    public function createAction(Request $request)
    {
        $entity  = new Topic();
        $form = $this->createForm(new TopicType(), $entity);
        $form->bind($request);

            $repository =   $this->getDoctrine()->getManager()
                            ->getRepository('NuitInfo2013MainBundle:Tag');
            $em = $this->getDoctrine()->getManager();
            $entity->setUser($this->container->get('security.context')->getToken()->getUser());
            foreach(explode(" ",$request->get('tags_selected')) as $tag){
                $tag = $repository->findByTitle($tag);
                if($tag != null)
                    $entity->addTags($tag[0]);
            }
            $em->persist($entity);
            $em->flush();

            return $this->indexAction();
    }

    /**
     * Displays a form to create a new Topic entity.
     *
     */
    public function newAction()
    {
        $entity = new Topic();
        $form   = $this->createForm(new TopicType(), $entity);

        return $this->render('NuitInfo2013MainBundle:Topic:new.html.twig', array(
            'entity' => $entity,
            'form'   => $form->createView(),
        ));
    }

    /**
     * Finds and displays a Topic entity.
     *
     */
    public function showAction($id)
    {
        $em = $this->getDoctrine()->getManager();

        $entity = $em->getRepository('NuitInfo2013MainBundle:Topic')->find($id);

        if (!$entity) {
            throw $this->createNotFoundException('Unable to find Topic entity.');
        }

        $deleteForm = $this->createDeleteForm($id);

        return $this->render('NuitInfo2013MainBundle:Topic:show.html.twig', array(
            'entity'      => $entity,
            'delete_form' => $deleteForm->createView(),        ));
    }

    /**
     * Displays a form to edit an existing Topic entity.
     *
     */
    public function editAction($id)
    {
        $em = $this->getDoctrine()->getManager();

        $entity = $em->getRepository('NuitInfo2013MainBundle:Topic')->find($id);

        if (!$entity) {
            throw $this->createNotFoundException('Unable to find Topic entity.');
        }

        $editForm = $this->createForm(new TopicType(), $entity);
        $deleteForm = $this->createDeleteForm($id);

        return $this->render('NuitInfo2013MainBundle:Topic:edit.html.twig', array(
            'entity'      => $entity,
            'form'   => $editForm->createView(),
        ));
    }

    /**
     * Edits an existing Topic entity.
     *
     */
    public function updateAction(Request $request, $id)
    {
        $em = $this->getDoctrine()->getManager();

        $entity = $em->getRepository('NuitInfo2013MainBundle:Topic')->find($id);

        $repository =   $this->getDoctrine()->getManager()
                            ->getRepository('NuitInfo2013MainBundle:Tag');

        $editForm = $this->createForm(new TopicType(), $entity);
        $editForm->bind($request);
        $entity->removeAllTags();
        foreach(explode(" ",$request->get('tags_selected')) as $tag){
            $tag = $repository->findByTitle($tag);
            if($tag != null)
                $entity->addTags($tag[0]);
        }
        $em->persist($entity);
        $em->flush();

        return $this->indexAction();
    }
    /**
     * Deletes a Topic entity.
     *
     */
    public function deleteAction($id)
    {
        $em = $this->getDoctrine()->getManager();
        $entity = $em->getRepository('NuitInfo2013MainBundle:Topic')->find($id);
        $em->remove($entity);
        $em->flush();
        return $this->indexAction();
    }

    /**
     * Creates a form to delete a Topic entity by id.
     *
     * @param mixed $id The entity id
     *
     * @return \Symfony\Component\Form\Form The form
     */
    private function createDeleteForm($id)
    {
        return $this->createFormBuilder(array('id' => $id))
            ->add('id', 'hidden')
            ->getForm()
        ;
    }

    public function findAction(){
        $request = $this->get('request');
            $repository =   $this->getDoctrine()
                            ->getManager()
                            ->getRepository('NuitInfo2013MainBundle:Tag');
            $entities = $repository->findBySearch($request->get('search'));
            return $this->render('NuitInfo2013MainBundle:Topic:tag_results.html.twig', array('entities'=> $entities));
    }
}
